import pygame, sys, math
pygame.init()

clock = pygame.time.Clock()

def distance(pt1, pt2):
	x1 = pt1[0]
	y1 = pt1[1]
	x2 = pt2[0]
	y2 = pt2[1]
	
	return math.sqrt(((x2-x1)**2)+((y2-y1)**2))

width = 640
height = 480
size = width, height

screen = pygame.display.set_mode(size)

bgColor = r,g,b = 0,0,0

ball1Image = pygame.image.load("ball.png")
ball1Rect = ball1Image.get_rect()
speed1x = 2
speed1y = 3
ball1Speed = [speed1x, speed1y]
ball1Radius = ball1Rect.width/2

ball2Image = pygame.image.load("ball.png")
ball2Rect = ball2Image.get_rect()
speed2x = 3
speed2y = 3
ball2Speed = [speed2x, speed2y]
ball2Radius = ball2Rect.width/2

ball3Image = pygame.image.load("ball.png")
ball3Rect = ball3Image.get_rect()
speed3x = 3
speed3y = 3
ball3Speed = [speed3x, speed3y]
ball3Radius = ball3Rect.width/2


ballPImage = pygame.image.load("playerBall.png")
ballPRect = ballPImage.get_rect()
speedPx = 0
speedPy = 0
ballPSpeed = [speedPx, speedPy]
ballPRadius = ballPRect.width/2

ballP2Image = pygame.image.load("playerBall.png")
ballP2Rect = ballP2Image.get_rect()
speedP2x = 0
speedP2y = 0
ballP2Speed = [speedPx, speedPy]
ballP2Radius = ballP2Rect.width/2

ball3Rect = ball3Rect.move(10,150)
ball2Rect = ball2Rect.move(150,150)
ballPRect = ballPRect.move(width/2,height-100)
ballP2Rect = ballP2Rect.move(width/2,height-100)

while True:
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			sys.exit()
		if event.type == pygame.KEYDOWN:
			if event.key == pygame.K_d:
				ballPSpeed[0] = 3
			if event.key == pygame.K_RIGHT:
				ballP2Speed[0] = 3
			if event.key == pygame.K_a:
				ballPSpeed[0] = -3
			if event.key == pygame.K_LEFT:
				ballP2Speed[0] = -3
			if event.key == pygame.K_w:
				ballPSpeed[1] = -3
			if event.key == pygame.K_UP:
				ballP2Speed[1] = -3
			if event.key == pygame.K_s: 
				ballPSpeed[1] = 3
			if event.key == pygame.K_DOWN:
				ballP2Speed[1] = 3
		if event.type == pygame.KEYUP:
			if event.key == pygame.K_d:
				ballPSpeed[0] = 0
			if event.key == pygame.K_RIGHT:
				ballP2Speed[0] = 0
			if event.key == pygame.K_w:
				ballPSpeed[1] = 0
			if event.key == pygame.K_UP:
				ballP2Speed[1] = 0
			if event.key == pygame.K_s:
				ballPSpeed[1] = 0
			if event.key == pygame.K_DOWN:
				ballP2Speed[1] = 0
			if event.key == pygame.K_a:
				ballPSpeed[0] = 0
			if event.key == pygame.K_LEFT:
				ballP2Speed[0] = 0
		  
	ball1Rect = ball1Rect.move(ball1Speed)
	ball2Rect = ball2Rect.move(ball2Speed)
	ball3Rect = ball3Rect.move(ball3Speed)
	ballPRect = ballPRect.move(ballPSpeed)
	ballP2Rect = ballP2Rect.move(ballP2Speed)
	
	if ball1Rect.left < 0 or ball1Rect.right > width:
		ball1Speed[0] = -ball1Speed[0]
	if ball1Rect.top < 0 or ball1Rect.bottom > height:
		ball1Speed[1] = -ball1Speed[1]
		
	if ball2Rect.left < 0 or ball2Rect.right > width:
		ball2Speed[0] = -ball2Speed[0]
	if ball2Rect.top < 0 or ball2Rect.bottom > height:
		ball2Speed[1] = -ball2Speed[1]
		
	if ball3Rect.left < 0 or ball3Rect.right > width:
		ball3Speed[0] = -ball3Speed[0]
	if ball3Rect.top < 0 or ball3Rect.bottom > height:
		ball3Speed[1] = -ball3Speed[1]
		
	if ballPRect.left < 0 or ballPRect.right > width:
		ballPSpeed[0] = 0
	if ballPRect.top < 0 or ballPRect.bottom > height:
		ballPSpeed[1] = 0
		
	if ballP2Rect.left < 0 or ballP2Rect.right > width:
		ballP2Speed[0] = 0
	if ballP2Rect.top < 0 or ballP2Rect.bottom > height:
		ballP2Speed[1] = 0
		
	if ball1Rect.right > ball2Rect.left and ball1Rect.left < ball2Rect.right:
		if ball1Rect.bottom > ball2Rect.top and ball1Rect.top < ball2Rect.bottom:
			print ball1Radius + ball2Radius, distance(ball1Rect.center, ball2Rect.center)
			if ball1Radius + ball2Radius > distance(ball1Rect.center, ball2Rect.center):
				ball1Speed[0] = -ball1Speed[0]
				ball1Speed[1] = -ball1Speed[1]
				ball2Speed[0] = -ball2Speed[0]
				ball2Speed[1] = -ball2Speed[1]
			  
			  
	if ball3Rect.right > ball2Rect.left and ball3Rect.left < ball2Rect.right:
		if ball3Rect.bottom > ball2Rect.top and ball3Rect.top < ball2Rect.bottom:
			print ball3Radius + ball2Radius, distance(ball3Rect.center, ball2Rect.center)
			if ball3Radius + ball2Radius > distance(ball3Rect.center, ball2Rect.center):
				ball3Speed[0] = -ball3Speed[0]
				ball3Speed[1] = -ball3Speed[1]
				ball2Speed[0] = -ball2Speed[0]
				ball2Speed[1] = -ball2Speed[1]
	
	if ball3Rect.right > ball1Rect.left and ball3Rect.left < ball1Rect.right:
		if ball3Rect.bottom > ball1Rect.top and ball3Rect.top < ball1Rect.bottom:
			print ball3Radius + ball1Radius, distance(ball3Rect.center, ball1Rect.center)
			if ball3Radius + ball1Radius > distance(ball3Rect.center, ball1Rect.center):
				ball3Speed[0] = -ball3Speed[0]
				ball3Speed[1] = -ball3Speed[1]
				ball1Speed[0] = -ball1Speed[0]
				ball1Speed[1] = -ball1Speed[1]
			  
					
	if ballPRect.right > ball1Rect.left and ballPRect.left < ball1Rect.right:
		if ballPRect.bottom > ball1Rect.top and ballPRect.top < ball1Rect.bottom:
			if ballPRadius + ball1Radius > distance(ballPRect.center, ball1Rect.center):
				ball1Speed[0] = -ball1Speed[0]
				ball1Speed[1] = -ball1Speed[1]
				
	if ballPRect.right > ball2Rect.left and ballPRect.left < ball2Rect.right:
		if ballPRect.bottom > ball2Rect.top and ballPRect.top < ball2Rect.bottom:
			if ballPRadius + ball2Radius > distance(ballPRect.center, ball2Rect.center):
				ball2Speed[0] = -ball2Speed[0]
				ball2Speed[1] = -ball2Speed[1]
	
	if ballPRect.right > ball3Rect.left and ballPRect.left < ball3Rect.right:
		if ballPRect.bottom > ball3Rect.top and ballPRect.top < ball3Rect.bottom:
			if ballPRadius + ball3Radius > distance(ballPRect.center, ball3Rect.center):
				ball3Speed[0] = -ball3Speed[0]
				ball3Speed[1] = -ball3Speed[1]
	
	if ballP2Rect.right > ball1Rect.left and ballP2Rect.left < ball1Rect.right:
		if ballP2Rect.bottom > ball1Rect.top and ballP2Rect.top < ball1Rect.bottom:
			if ballP2Radius + ball1Radius > distance(ballP2Rect.center, ball1Rect.center):
				ball1Speed[0] = -ball1Speed[0]
				ball1Speed[1] = -ball1Speed[1]
				
	if ballP2Rect.right > ball2Rect.left and ballP2Rect.left < ball2Rect.right:
		if ballP2Rect.bottom > ball2Rect.top and ballP2Rect.top < ball2Rect.bottom:
			if ballP2Radius + ball2Radius > distance(ballP2Rect.center, ball2Rect.center):
				ball2Speed[0] = -ball2Speed[0]
				ball2Speed[1] = -ball2Speed[1]
	
	if ballP2Rect.right > ball3Rect.left and ballP2Rect.left < ball3Rect.right:
		if ballP2Rect.bottom > ball3Rect.top and ballP2Rect.top < ball3Rect.bottom:
			if ballP2Radius + ball3Radius > distance(ballP2Rect.center, ball3Rect.center):
				ball3Speed[0] = -ball3Speed[0]
				ball3Speed[1] = -ball3Speed[1]
		
	screen.fill(bgColor)
	screen.blit(ball1Image, ball1Rect)
	screen.blit(ball2Image, ball2Rect)
	screen.blit(ball3Image, ball3Rect)
	screen.blit(ballPImage, ballPRect)
	screen.blit(ballP2Image, ballP2Rect)
	pygame.display.flip()
	clock.tick(60)
	
	
	
	
	
	
	
	
	
	
	
			
	
	
	
	
	
	
	
	
	
	
 
 
 
 
 